package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    /**
     * 用户数据分页
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<UserVo>> findPage(@RequestBody UserDto userDto,
                                                         @PathVariable("pageNum") Integer pageNum,
                                                         @PathVariable("pageSize") Integer pageSize){
        PageResponse<UserVo> pageResponse = userService.findPage(userDto,pageNum,pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 添加用户
     * @param userDto
     * @return
     */
    @PutMapping
    public ResponseResult insertUser(@RequestBody UserDto userDto){
        Boolean flag = userService.insertUser(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 根据用户id删除数据
     * @param userIds
     * @return
     */
    @DeleteMapping("/remove/{userIds}")
    public ResponseResult deleteByUserId(@PathVariable String userIds){
        userService.deleteByUserId(userIds);
        return ResponseResult.success();
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult updateStatus(@PathVariable Long id,@PathVariable String status){
        userService.updateStatus(id,status);
        return ResponseResult.success();
    }

    /**
     * 密码重置
     * @param userId
     * @return
     */
    @PostMapping("/reset-passwords/{userId}")
    public ResponseResult resetPassword(@PathVariable String userId){
        Boolean flag =userService.resetPassword(userId);
        return ResponseResult.success(flag);
    }


    @PatchMapping
    public ResponseResult updateUser(@RequestBody UserDto userDto){
        Boolean flag =userService.updateUser(userDto);
        return ResponseResult.success(flag);
    }

    @PostMapping("/list")
    public ResponseResult findList(){
        List<UserVo> list = userService.findList();
        return ResponseResult.success(list);
    }
}
